package com.anyou.mall.goo.goodsAudit.dto;

import cn.hutool.core.text.CharSequenceUtil;
import com.anyou.common.core.domain.BaseEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.Map;

/**
 * 商品查询条件
 *
 * @author pikachu
 * @since 2020-02-24 19:27:20
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class GoodsAuditSearchParams extends BaseEntity {

    private static final long serialVersionUID = 2544015852728566887L;

    @ApiModelProperty(value = "商品编号")
    private String goodsId;

    @ApiModelProperty(value = "商品名称")
    private String goodsName;

    @ApiModelProperty(value = "商品编号")
    private String id;

    @ApiModelProperty(value = "商家ID")
    private String storeId;

    @ApiModelProperty(value = "卖家名字")
    private String storeName;

    @ApiModelProperty(value = "价格,可以为范围，如10_1000")
    private String price;

    @ApiModelProperty(value = "分类path")
    private String categoryPath;

    /*@ApiModelProperty(value = "店铺分类id")
    private String storeCategoryPath;*/

    /*@ApiModelProperty(value = "是否自营")
    private Boolean selfOperated;*/

    /**
     * 上架状态（0上架 1下架）
     */
    @ApiModelProperty(value = "上架状态（0上架 1下架）")
    private String marketEnable;

    /**
     * 审核状态（0待审核 1通过 2不通过）
     */
    @ApiModelProperty(value = "审核状态（0待审核 1通过 2不通过）")
    private String authFlag;

    /*@ApiModelProperty(value = "库存数量")
    private Integer leQuantity;*/

    /*@ApiModelProperty(value = "库存数量")
    private Integer geQuantity;*/

    @ApiModelProperty(value = "是否为推荐商品")
    private Boolean recommend;

    /**
     *
     */
    @ApiModelProperty(value = "商品类型")
    private String goodsType;

    /**
     *
     */
    /*@ApiModelProperty(value = "销售模式", required = true)
    private String salesModel;*/

    public <T> QueryWrapper<T> queryWrapper() {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        if (CharSequenceUtil.isNotEmpty(goodsId)) {
            queryWrapper.eq("goods_id", goodsId);
        }
        if (CharSequenceUtil.isNotEmpty(goodsName)) {
            queryWrapper.like("goods_name", goodsName);
        }
        if (CharSequenceUtil.isNotEmpty(id)) {
            queryWrapper.in("id", Arrays.asList(id.split(",")));
        }
        if (CharSequenceUtil.isNotEmpty(storeId)) {
            queryWrapper.eq("store_id", storeId);
        }
        if (CharSequenceUtil.isNotEmpty(storeName)) {
            queryWrapper.like("store_name", storeName);
        }
        if (CharSequenceUtil.isNotEmpty(categoryPath)) {
            queryWrapper.like("category_path", categoryPath);
        }
        /*if (selfOperated != null) {
            queryWrapper.eq("self_operated", selfOperated);
        }*/
        if (CharSequenceUtil.isNotEmpty(marketEnable)) {
            queryWrapper.eq("market_enable", marketEnable);
        }
        if (CharSequenceUtil.isNotEmpty(authFlag)) {
            queryWrapper.eq("auth_flag", authFlag);
        }
        /*if (leQuantity != null) {
            queryWrapper.le("quantity", leQuantity);
        }
        if (geQuantity != null) {
            queryWrapper.ge("quantity", geQuantity);
        }*/
        if (recommend != null) {
            queryWrapper.le("recommend", recommend);
        }
        if (CharSequenceUtil.isNotEmpty(goodsType)) {
            queryWrapper.eq("goods_type", goodsType);
        }
        // 时间搜索
        for (Map.Entry<String, Object> map : getParams().entrySet()){
            if (map.getKey().equals("beginTime") && map.getValue() != null) {
                queryWrapper.ge("create_time", map.getValue());
            }
            if (map.getKey().equals("endTime") && map.getValue() != null) {
                queryWrapper.le("create_time", map.getValue());
            }
        }
        /*if (CharSequenceUtil.isNotEmpty(salesModel)) {
            queryWrapper.eq("sales_model", salesModel);
        }*/

        queryWrapper.eq("del_flag", 0);
        this.betweenWrapper(queryWrapper);
        return queryWrapper;
    }

    private <T> void betweenWrapper(QueryWrapper<T> queryWrapper) {
        if (CharSequenceUtil.isNotEmpty(price)) {
            String[] s = price.split("_");
            if (s.length > 1) {
                queryWrapper.between("price", s[0], s[1]);
            } else {
                queryWrapper.ge("price", s[0]);
            }
        }
    }


}
